//
//  SFCountdownView.h
//  Pod
//
//  Created by Thomas Winkler on 10/02/14.
//  Copyright (c) 2014 SimpliFlow. All rights reserved.
//

#import "SFCountdownView.h"

@interface SFCountdownView ()

@property (nonatomic) NSTimer* timer;
@property (nonatomic) UILabel* countdownLabel;

@property (nonatomic) int currentCountdownValue;

@end

#define COUNTDOWN_LABEL_FONT_SCALE_FACTOR 0.25

@implementation SFCountdownView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.countdownColor = [UIColor colorWithWhite:0.848 alpha:1.000];
//        self.countdownLabel.frame = CGRectMake(self.frame.size.width / 2 - 30, self.frame.size.height / 2 - 40, 60, 80);
//        NSLog(@"%f", _countdownLabel.frame.origin.x);
//        NSLog(@"%f", _countdownLabel.frame.origin.y);
//        NSLog(@"%f", _countdownLabel.frame.size.width);
//        NSLog(@"%f", _countdownLabel.frame.size.height);
        [self updateAppearance];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width , 20)];
        label.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 3+ 18);
        label.text = @"11\"~ 8\'";
        label.font = [UIFont systemFontOfSize:22];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        [self addSubview:label];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aCoder {
    if(self = [super initWithCoder:aCoder]){
        [self updateAppearance];
    }
    return self;
}

- (void) updateAppearance
{
    [[self subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    self.opaque = NO;
    self.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.610];
    
    // countdown label
    float fontSize = self.bounds.size.width * COUNTDOWN_LABEL_FONT_SCALE_FACTOR;
    
    self.countdownLabel = [[UILabel alloc] init];
    [self.countdownLabel setFont:[UIFont fontWithName:self.fontName size:fontSize]];
    [self.countdownLabel setTextColor:self.countdownColor];
    self.countdownLabel.textAlignment = NSTextAlignmentCenter;
    self.countdownLabel.frame = CGRectMake(0, -100, self.frame.size.width, self.frame.size.height);
//    [self.countdownLabel setCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 4)];
    
    self.countdownLabel.opaque = YES;
    self.countdownLabel.alpha = 1.0;
    [self addSubview: self.countdownLabel];
    
}

- (void) layoutSubviews
{
    [super layoutSubviews];
    
    self.countdownLabel.frame = CGRectMake(0, -210, self.frame.size.width, self.frame.size.height);
//    [self.countdownLabel setCenter:self.center];
    [self.countdownLabel setCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 5)];
}

#pragma mark - start/stopping
- (void) start
{
    [self stop];
    self.currentCountdownValue = self.countdownFrom;
    
    self.countdownLabel.text = [NSString stringWithFormat:@"%d", self.countdownFrom];
    [self animate];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                  target:self
                                                selector:@selector(animate)
                                                userInfo:nil
                                                 repeats:YES];
}

- (void) stop
{
    if (self.timer && [self.timer isValid]) {
        [self.timer invalidate];
    }
}

#pragma mark - animation stuff

- (void) animate
{
    [UIView animateWithDuration:0.9 animations:^{
    
        CGAffineTransform transform = CGAffineTransformMakeScale(1.5, 1.5);
        self.countdownLabel.transform = transform;
        self.countdownLabel.alpha = 0;
        
    } completion:^(BOOL finished) {
        if (finished) {
            
             if (self.currentCountdownValue == 1) {
                 [self stop];
                 if (self.delegate) {
                     [self.delegate countdownFinished:self];
                     [self removeFromSuperview];
                 }
                 
             } else {

                self.countdownLabel.transform = CGAffineTransformIdentity;
                self.countdownLabel.alpha = 1.0;
                
                self.currentCountdownValue--;
                 
                 if (_currentCountdownValue > 0) {
                     if (self.currentCountdownValue == 1) {
                         self.countdownLabel.text = self.finishText;
                     } else {
                         self.countdownLabel.text = [NSString stringWithFormat:@"%d", self.currentCountdownValue ];
                     }
                 }
            }
        }
    }];
}

#pragma mark - custom getters
- (NSString*)finishText
{
    if (!_finishText) {
        _finishText = @"Go";
    }
    
    return _finishText;
}

- (float)backgroundAlpha
{
    if (_backgroundAlpha == 0) {
        _backgroundAlpha = 0.3;
    }
    
    return _backgroundAlpha;
}

- (int) countdownFrom
{
    if (_countdownFrom == 0) {
        _countdownFrom = kDefaultCountdownFrom;
    }
    
    return _countdownFrom;
}

- (UIColor*)countdownColor
{
    if (!_countdownColor) {
        _countdownColor = [UIColor blackColor];
    }
    
    return _countdownColor;
}

- (NSString *)fontName
{
    if (!_fontName) {
        _fontName = @"HelveticaNeue-Medium";
    }
    
    return _fontName;
}



@end
